#ifndef SQSTACK_H
#define SQSTACK_H

#include "AStack.h"
template<class T>
class SqStack :public AStack<T>{
private:
	T * base1, *top1;
	int stacksize;
public:
	SqStack(int k = 1){
		base1 = new T[k];
		assert(base1 != NULL);
		top1 = base1;
		stacksize = k;
	}
	~SqStack(){
		delete[] base1;
	}
	void ClearStack(){
		top1 = base1;
	}
	bool StackEmpty()const{
		return top1 == base1;
	}
	int StackLength()const{
		return top1 - base1;
	}
	bool GetTop(T &e)const{
		if (top1 > base1){
			e = *(top1 - 1);
			return true;
		}
		else{
			return false;
		}
	}
	bool Push(T e){
		if (top1 - base1 == stacksize){
			T* newbase = new T[stacksize * 2];
			if (newbase == NULL){
				return false;
			}
			for (int j = 0; j < top1 - base1; j++){
				*(newbase + j) = *(base1 + j);
			}
			delete[] base1;
			base1 = newbase;
			top1 = base1 + stacksize;
			stacksize *= 2;
		}
		*top1 = e;
		top1++;
		return true;
	}
	bool Pop(T &e){
		if (top1 == base1)
			return false;
		--top1;
		e = *top1;
		return true;
	}
	void StackTraverse(void(*visit)(T*))const{
		T *p = base1;
		while (p < top1){
			visit(p);
			p++;
		}
		cout << endl;
	}
};

#endif